import subprocess
import piclib
import normal
from airtest.core.api import *
from normal import delay_ms
import sys
import cv2


def solve_unread(tap_refresh=(540, 666), compared_area=None, compared_png='pic.png', tap_unread=(0, 0)):
    unread_ = True
    if compared_area is None:
        compared_area = [1, 2, 3, 4]
    touch(tap_refresh, times=2)
    delay_ms(100)
    snapshot(shot_pic)
    print('截图完成')
    origin_pic_ = cv2.imread(shot_pic)
    resized_pic_ = cv2.resize(origin_pic_, (1080, 2160))
    pic_cut_ = resized_pic_[compared_area[1]:compared_area[3], compared_area[0]:compared_area[2]]
    cv2.imwrite(cut_name, pic_cut_)
    if win_flag:
        fixed_pic = 'F:\\coede_test\\python\\airtest_project\\pic\\' + compared_png
        compared_res_ = piclib.compare_pic(fixed_pic, cut_name)
    else:
        fixed_pic = 'pic/' + compared_png
        compared_res_ = piclib.compare_pic(fixed_pic, cut_name)
    if compared_res_ < 10:
        print('所有消息已读,期待下一次的使用,bye~')
        unread_ = False
    else:
        touch(tap_unread)
        delay_ms(300)
        keyevent('BACK')
    return unread_


unread = True
system = normal.detect_system()
if len(sys.argv) > 1:
    app = sys.argv[1]  # 读取是qq还是wechat
else:
    app = 'wechat'
print(app)
win_flag = True
if system == 'ubuntu':
    win_flag = False
    if not os.path.exists('pic'):
        subprocess.getstatusoutput('mkdir pic')
    shot_pic = 'pic/shot.png'
    cut_name = 'pic/cut.png'
else:
    win_flag = True
    shot_pic = 'F:\\coede_test\\python\\airtest_project\\pic\\shot.png'
    cut_name = 'F:\\coede_test\\python\\airtest_project\\pic\\cut.png'
if app == 'qq':
    if win_flag:
        unexpect_pic = 'F:\\coede_test\\python\\airtest_project\\pic\\cut_unexpect.png'
    else:
        unexpect_pic = 'pic/cut_unexpect.png'
# connect an android phone with adb
init_device('Android', '872QEEUG2255G')
print('连接成功')
while unread:
    if app == 'wechat':
        unread = solve_unread((133, 1976), [88, 1930, 212, 2069], 'cut_wechat.png', (666, 316))
    elif app == 'taobao':
        unread = solve_unread((540, 1986), [489, 1928, 626, 2068], 'cut_taobao.png', (666, 328))
    elif app == 'qq':
        touch((186, 1984), times=2)
        delay_ms(500)
        snapshot(shot_pic)
        print('截图完成')
        origin_pic = cv2.imread(shot_pic)
        resized_pic = cv2.resize(origin_pic, (1080, 2160))
        pic_cut = resized_pic[1911:2065, 133:264]
        cv2.imwrite(cut_name, pic_cut)
        if win_flag:
            compared_res = piclib.compare_pic('F:\\coede_test\\python\\airtest_project\\pic\\cut_qq.png', cut_name)
        else:
            compared_res = piclib.compare_pic('pic/cut_qq.png', cut_name)
        if compared_res < 10:
            print('所有消息已读,期待下一次的使用,bye~')
            unread = False
        else:
            unexpect_cut = resized_pic[255:416, 44:1045]

            cv2.imwrite(unexpect_pic, unexpect_cut)
            if win_flag:
                additional_compare = piclib.compare_pic('F:\\coede_test\\python\\airtest_project\\pic\\qq_unexpect.png',
                                                        unexpect_pic)
            else:
                additional_compare = piclib.compare_pic('pic/qq_unexpect.png', unexpect_pic)
            if additional_compare > 20:
                touch((666, 320))
                delay_ms(300)
                keyevent('BACK')
    delay_ms(350)
# start_app('com.wisedu.cpdaily')
# print(res)

# (86,1929)   (209,2062)
# cut = piclib.pic_cut(shot_pic, [86, 1929, 209, 2062])

# 文字提取效果不好,换成比较图片
# piclib.pic_binarization(cut,binary_pic,20)
# print('裁剪完成')
# text=piclib.extract_text(binary_pic)
# print(text)
